﻿// **********************************************************************
//
// Author: KingSley
// Email:3268374537@qq.com
// Created:	2018/8/1   09:10
// Introduce: 继承MonoBehaviour的单例继承此类
//
// **********************************************************************
using UnityEngine;

namespace KingSley
{
    public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
    {
        private static T m_Instance = null;

        public static T Instance()
        {
            if (m_Instance == null)
            {
                m_Instance = GameObject.FindObjectOfType(typeof(T)) as T;
                if (m_Instance == null)
                {
                    m_Instance = new GameObject(typeof(T).ToString(), typeof(T)).GetComponent<T>();
                    m_Instance.Init();
                }
            }
            return m_Instance;
        }

        private void Awake()
        {
            if (m_Instance == null)
            {
                m_Instance = this as T;
                Init();
            }
        }

        public virtual void Init() { }

        public void AddGameObject<T2>() where T2 : MonoBehaviour
        {
            var go = new GameObject();
            go.name = typeof(T2).ToString();
            go.AddComponent<T2>();
            go.transform.SetParent(transform);
        }
        public GameObject AddGameObject(string name)
        {
            var go = new GameObject();
            go.name = name;
            go.transform.SetParent(transform);
            return go;
        }

        private void OnApplicationQuit()
        {
            m_Instance = null;
        }
    }
}